﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpPunishmentWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_PunishmentModel model = new EmpbasicService.Emp_PunishmentModel();
        private EmpbasicService.Emp_PunishmentModel oldmodel = new EmpbasicService.Emp_PunishmentModel();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        public int ID;
        public int EmpID;
        public bool IsNew = false;
        public EmpPunishmentWindow(int empID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增处分信息";
                model = new EmpbasicService.Emp_PunishmentModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑处分信息";
                Client.GetEmp_PunishmentModelCompleted += new EventHandler<EmpbasicService.GetEmp_PunishmentModelCompletedEventArgs>(Client_GetEmp_PunishmentModelCompleted);
                Client.GetEmp_PunishmentModelAsync("(" + EmpID + ")", ID);
            }
        }

        void Client_GetEmp_PunishmentModelCompleted(object sender, EmpbasicService.GetEmp_PunishmentModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_PunishmentModel
                {
                    PunishmentNo = model.PunishmentNo,
                    PunishmentName = model.PunishmentName,
                    PunishmentReason = model.PunishmentReason,
                    OkUnit = model.OkUnit,
                    OKDate = model.OKDate,
                    PunishmentTimeLimit = model.PunishmentTimeLimit
                };
            }
            this.DataContext = model;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;
                Client.AddEmp_PunishmentModelCompleted += new EventHandler<EmpbasicService.AddEmp_PunishmentModelCompletedEventArgs>(Client_AddEmp_PunishmentModelCompleted);
                Client.AddEmp_PunishmentModelAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.OKDate = Convert.ToDateTime(begin.Text);
                else
                    model.OKDate = null;
                Client.UpdateEmp_PunishmentModelCompleted += new EventHandler<EmpbasicService.UpdateEmp_PunishmentModelCompletedEventArgs>(Client_UpdateEmp_PunishmentModelCompleted);
                Client.UpdateEmp_PunishmentModelAsync(model);
            }

        }

        void Client_UpdateEmp_PunishmentModelCompleted(object sender, EmpbasicService.UpdateEmp_PunishmentModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_PunishmentModelCompleted(object sender, EmpbasicService.AddEmp_PunishmentModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("处分信息子集", "", "", model.EmpBasicID, FangShi, "");
            }
            else
            {
                if (oldmodel.OKDate != model.OKDate)
                    insertsys.Insert("处分信息子集", oldmodel.OKDate == null ? null : Convert.ToDateTime(oldmodel.OKDate).ToShortDateString(),
                        model.OKDate == null ? null : Convert.ToDateTime(model.OKDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "处分批准日期");
                if (oldmodel.PunishmentNo != model.PunishmentNo)
                    insertsys.Insert("处分信息子集", oldmodel.PunishmentNo, model.PunishmentNo,
                        model.EmpBasicID, FangShi, "处分代码");
                if (oldmodel.PunishmentName != model.PunishmentName)
                    insertsys.Insert("处分信息子集", oldmodel.PunishmentName, model.PunishmentName,
                        model.EmpBasicID, FangShi, "处分名称");

                if (oldmodel.PunishmentReason != model.PunishmentReason)
                    insertsys.Insert("处分信息子集", oldmodel.PunishmentReason, model.PunishmentReason,
                        model.EmpBasicID, FangShi, "处分原因");
                if (oldmodel.OkUnit != model.OkUnit)
                    insertsys.Insert("处分信息子集", oldmodel.OkUnit, model.OkUnit,
                        model.EmpBasicID, FangShi, "处分批准机关名称");
                if (oldmodel.PunishmentTimeLimit != model.PunishmentTimeLimit)
                    insertsys.Insert("处分信息子集", oldmodel.PunishmentTimeLimit, model.PunishmentTimeLimit,
                        model.EmpBasicID, FangShi, "处分期限");
            }

        }
    }
}
